home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-02-21 | 457 b | 35 lines | [TEXT/RLAB] |
- //
- // The 3n + 1 problem
- //
-
- collatz = function(start)
- {
- local(c, n);
- c = n = start;
-
- while(n > 1)
- {
- if(mod(n,2) == 0) {
- n = n/2;
- else
- n = 3*n + 1;
- }
- c = [c, n];
- }
- return c;
- };
-
- //
- // Try it out
- //
- c = collatz(100);
-
- plgrid ();
- ptitle ( "RLaB Collatz Example" );
- xlabel ( "Independent Variable" );
- ylabel ( "Dependent Variable" );
- plot( [1:c.nc; c]' );
- pause ();
- plgrid ( ,"bcgnstlv");
- plot( [1:c.nc; c]' );
-